package com.newtype.meeting.client.widget.backend;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Degree;
import com.newtype.meeting.client.model.Title;
import com.newtype.meeting.client.widget.SubjectHtml;
import com.newtype.meeting.shared.AccountDTO;
import com.newtype.meeting.shared.tools.RadioGroupConverter;

public class OpenedNewAccountPanel extends ContentPanel {

	private BeanModel accountModel;

	private HiddenField<String> deptID;//部门ID
	private TextField<String> deptName;//部门名称Name
	private HiddenField<String> partTimeID;//兼职部门ID
	private TextField<String> partTimeName;//兼职部门Name
	private ComboBox<Degree> degree;//职级
	private ComboBox<Title> titleCombo;//职称

	public OpenedNewAccountPanel(AccountDTO accountDTO, List<Degree> degrees,
			final List<Title> titles) {
		//		setHeading("开通新帐户");
		setHeaderVisible(false);
		setLayout(new FormLayout());
		setScrollMode(Scroll.AUTOY);
		setButtonAlign(HorizontalAlignment.CENTER);

		createFormPanel(accountDTO, degrees, titles);
	}

	private void createFormPanel(AccountDTO accountDTO, List<Degree> degrees,
			final List<Title> titles) {
		accountModel = BeanModelLookup.get().getFactory(AccountDTO.class)
				.createModel(accountDTO);

		FormData formData = new FormData("98%");
		FormPanel form = new FormPanel();
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		form.setFrame(true);
		form.setButtonAlign(HorizontalAlignment.CENTER);
		form.setStyleAttribute("margin", "10px 150px 10px 150px");

		//页面主题
		String imgsrc = "images/docedit.png";
		SubjectHtml subjectHtml = new SubjectHtml("开通新进员工帐号", imgsrc, "", "",
				"");
		form.add(subjectHtml, formData);

		//姓名
		LabelField fullName = new LabelField();
		fullName.setFieldLabel("姓名:");
		fullName.setName("fullName");
		form.add(fullName);
		//帐号
		LabelField accountID = new LabelField();
		accountID.setFieldLabel("帐号:");
		accountID.setName("accountID");
		form.add(accountID);
		//性别
		Radio radio1 = new Radio();
		radio1.setBoxLabel("男");
		radio1.setValueAttribute("1");

		Radio radio2 = new Radio();
		radio2.setBoxLabel("女");
		radio2.setValueAttribute("0");

		RadioGroup gender = new RadioGroup();
		gender.setFieldLabel("性别");
		gender.setName("gender");
		gender.add(radio1);
		gender.add(radio2);
		if ("1".equals(accountDTO.getGender())) {
			gender.setValue(radio1);
		} else {
			gender.setValue(radio2);
		}
		form.add(gender);

		//生日
		DateField birthday = new DateField();
		birthday.setFieldLabel("生日");
		birthday.setName("birthday");
		form.add(birthday);

		//到职日期
		DateField onlineDATE = new DateField();
		onlineDATE.setFieldLabel("到职日期");
		onlineDATE.setName("onlineDATE");
		form.add(onlineDATE);

		//职称
		final ListStore<Title> titleStore = new ListStore<Title>();
		titleStore.add(new ArrayList<Title>());

		titleCombo = new ComboBox<Title>();
		titleCombo.setFieldLabel("职称");
		titleCombo.setEmptyText("请选择一个职称...");
		titleCombo.setEditable(false);
		titleCombo.setTriggerAction(TriggerAction.ALL);
		titleCombo.setDisplayField("titleCaption");
		titleCombo.setValueField("id");
		titleCombo.setStore(titleStore);

		final ListStore<Degree> degreeStore = new ListStore<Degree>();
		degreeStore.add(degrees);

		//职级
		degree = new ComboBox<Degree>();
		degree.setFieldLabel("职级");
		degree.setEmptyText("请选择职级...");
		degree.setEditable(false);
		degree.setTriggerAction(TriggerAction.ALL);
		degree.setDisplayField("degreeID");
		degree.setStore(degreeStore);
		degree
				.addSelectionChangedListener(new SelectionChangedListener<Degree>() {
					@Override
					public void selectionChanged(
							final SelectionChangedEvent<Degree> se) {
						titleCombo.clearSelections();
						titleCombo.getStore().removeAll();
						final List<Title> list = new ArrayList<Title>();
						Iterator<Title> iter = titles.iterator();
						while (iter.hasNext()) {
							Title title = iter.next();
							if (title.get("degree") == se.getSelectedItem()
									.get("degreeID")) {
								list.add(title);
							}
						}
						titleStore.add(list);
					}
				});
		form.add(degree);
		form.add(titleCombo);

		//专职部门ID
		deptID = new HiddenField<String>();
		deptID.setFieldLabel("部门ID");
		deptID.setName("deptID");
		form.add(deptID);

		//专职部门
		deptName = new TextField<String>();
		deptName.setFieldLabel("专职部门");
		deptName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> department = new HashMap<String, Field<String>>();
				department.put("pDeptID", deptID);
				department.put("pDeptName", deptName);
				Dispatcher.forwardEvent(AppEvents.ToDeptTreeSingleWindow,
						department);
			}
		});
		form.add(deptName);

		//兼职部门ID
		partTimeID = new HiddenField<String>();
		partTimeID.setFieldLabel("部门ID");
		partTimeID.setName("byWorkDeptID");
		form.add(partTimeID);

		//兼职部门
		partTimeName = new TextArea();
		partTimeName.setFieldLabel("兼职部门");
		partTimeName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> department = new HashMap<String, Field<String>>();
				department.put("deptID", partTimeID);
				department.put("deptName", partTimeName);
				Dispatcher.forwardEvent(AppEvents.ToDeptTreeMultiWindow,
						department);
			}
		});
		form.add(partTimeName, formData);

		//工作执掌
		TextArea workDuties = new TextArea();
		workDuties.setFieldLabel("工作职掌");
		workDuties.setName("workDuties");
		form.add(workDuties, formData);

		//公司电话
		TextField<String> officePhoneNO = new TextField<String>();
		officePhoneNO.setFieldLabel("公司电话");
		officePhoneNO.setName("officePhoneNO");
		form.add(officePhoneNO);

		//分机
		TextField<String> officephoneEXT = new TextField<String>();
		officephoneEXT.setFieldLabel("分机");
		officephoneEXT.setName("officephoneEXT");
		officephoneEXT.setWidth(100);
		form.add(officephoneEXT);

		//Email
		TextField<String> email = new TextField<String>();
		email.setFieldLabel("Email");
		email.setName("email");
		form.add(email, formData);

		//MSN
		TextField<String> msn = new TextField<String>();
		msn.setFieldLabel("MSN");
		msn.setName("msn");
		form.add(msn, formData);

		//Skype
		TextField<String> skype = new TextField<String>();
		skype.setFieldLabel("Skype");
		skype.setName("skype");
		form.add(skype, formData);

		//手机
		TextField<String> phone = new TextField<String>();
		phone.setFieldLabel("手机");
		phone.setName("phone");
		form.add(phone, formData);

		//呼叫器
		TextField<String> pager = new TextField<String>();
		pager.setFieldLabel("呼叫器");
		pager.setName("pager");
		form.add(pager, formData);

		//家用电话
		TextField<String> homePhoneNO = new TextField<String>();
		homePhoneNO.setFieldLabel("家用电话");
		homePhoneNO.setName("homePhoneNO");
		form.add(homePhoneNO, formData);

		//紧急联络人
		TextField<String> emergencyContact = new TextField<String>();
		emergencyContact.setFieldLabel("紧急联络人");
		emergencyContact.setName("emergencyContact");
		form.add(emergencyContact, formData);

		//紧急联络电话
		TextField<String> emergencyPhoneNO = new TextField<String>();
		emergencyPhoneNO.setFieldLabel("紧急联络电话");
		emergencyPhoneNO.setName("emergencyPhoneNO");
		form.add(emergencyPhoneNO, formData);

		//通信地址
		TextArea address = new TextArea();
		address.setFieldLabel("通信地址");
		address.setName("address");
		form.add(address, formData);

		//备注
		TextArea notes = new TextArea();
		notes.setName("notes");
		notes.setFieldLabel("备注");
		form.add(notes, formData);

		//绑定数据
		FormBinding binding = new FormBinding(form, true);
		//绑定性别RadioGroup
		FieldBinding genderFB = binding.getBinding(gender);
		genderFB.setConverter(new RadioGroupConverter(gender));
		binding.bind(accountModel);

		//送出按钮
		form.addButton(getSaveButton());
		//删除申请按钮
		form.addButton(getDeleteButton());
		add(form);
	}

	private Button getDeleteButton() {
		Button button = new Button("进行开通");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				AccountDTO account = accountModel.getBean();
				account.setDegree(String.valueOf(degree.getValue()
						.getDegreeID()));//职级
				account.setTitle(titleCombo.getValue().getId());//
				account.setDeptID(deptID.getValue());//专职部门ID
				account.setByWorkDeptID(partTimeID.getValue());//兼职部门ID
				Dispatcher.forwardEvent(AppEvents.OpenedNewAccount, account);
			}
		});
		return button;
	}

	private Button getSaveButton() {
		Button button = new Button("删除此申请");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				MessageBox.confirm("Confirm",
						"Are you sure you want to do that?",
						new Listener<MessageBoxEvent>() {
							@Override
							public void handleEvent(MessageBoxEvent be) {
								Button btn = be.getButtonClicked();
								if ("Yes".equals(btn.getText())
										|| "是".equals(btn.getText())) {
									AccountDTO account = accountModel.getBean();
									Dispatcher
											.forwardEvent(
													AppEvents.DeleteNewAccount,
													account);
								}
							}
						});
			}
		});
		return button;
	}
}
